<?php

namespace App\Repositories\Admin;

use App\Models\Admin\ActionModel;
use App\Repositories\BaseRepository;

class ActionRepository extends BaseRepository
{
    protected $repository;

    public function __construct()
    {
        $this->repository = new ActionModel();
    }

    /**
     * 操作分页获取
     */
    public function getIndexList($moduleId)
    {
        return $this->repository->where('pid',$moduleId)
            ->orderBy('sort','desc')
            ->paginate(10);
    }

    /**
     * 模块获取
     */
    public function getModuleList($moduleId)
    {
        return $this->repository->where('del',0)
            ->where('pid',$moduleId)
            ->get();
    }

    /**
     * 列表
     * 子菜单获取
     */
    public function getSubsList($pid)
    {
        return $this->repository
//            ->where('del',0)
            ->where('pid',$pid)
            ->get();
    }

    /**
     * 左侧菜单栏
     * 子菜单获取
     */
    public function getSubsList2($pid)
    {
        return $this->repository
            ->where('del',0)
            ->where('pid',$pid)
            ->get();
    }

    /**
     * 获取父级列表
     */
    public function getParentList($moduleId)
    {
        $models = ActionModel::where('del',0)
            ->where('id','>',1)
            ->where('pid',$moduleId)->get();
        $dataArr = array();
        if (!count($models)) { return $dataArr; }
        foreach ($models as $model) {
            $dataArr[$model->id] = $model->name;
//            $model2s = ActionModel::where('del',0)
//                ->where('pid',$model->id)->get();
//            if (count($model2s)) {
//                foreach ($model2s as $model2) {
//                    $dataArr[$model2->id] = $model2->name;
//                }
//            }
        }
        return $dataArr;
    }

    /**
     * 获取父级名称
     */
    public function getParentName($pid)
    {
        $model = ActionModel::find($pid);
        return $model ? $model->name : '顶级操作';
    }

    /**
     * 新增
     */
    public function setAdd($data)
    {
        return $this->repository->insertGetId($data);
    }

    /**
     * 修改
     */
    public function setModify($data,$id)
    {
        return $this->repository->where('id',$id)->update($data);
    }

    /**
     * 删除
     */
    public function setDel($id)
    {
        return $this->repository->where('id',$id)->update(['del'=>1]);
    }

    /**
     * 还原
     */
    public function setDelRedo($id)
    {
        return $this->repository->where('id',$id)->update(['del'=>0]);
    }

    /**
     * 获取一条记录
     */
    public function getShowOne($id)
    {
        return $this->repository->where('id',$id)->first();
    }

    /**
     * 获取首页名称
     */
    public function getHome()
    {
        return $this->repository->where('id',1)->first();
    }

    /**
     * 获取首页子菜单
     */
    public function getHomeSub()
    {
        return $this->repository->where('id','>',1)
                    ->where('pid',0)->get();
    }
}